<?php
declare (strict_types = 1);

namespace app\controller\api;

use app\validate\File;
use think\facade\Filesystem;

/**
 * @title 工具方法
 */
class Tool
{
    /**
     * @title 上传图片
     * @url /api/upload_file
     * @method post
     *
     * @param name:file type:file require:1 desc:文件
     * @param name:type type:integer require:0 default:1 desc:1图片；2非图片
     *
     * @return url:图片地址
     */
    public function uploadFile()
    {
        $params = request()->only(['file', 'type']);
        $params['file'] = request()->file('file');
        $params['type'] = $params['type'] ?? 1;
        if ($params['type'] == 1) {
            $validate = (new File())->scene('pic');
        } elseif ($params['type'] == 2) {
            $validate = (new File())->scene('file');
        } else {
            result(null, 0, '未知的文件类型');
        }
        if (is_array($params['file'])) {
            foreach ($params['file'] as $file) {
                if (!$validate->check(['file' => $file])) {
                    result(null, 0, $validate->getError());
                }
            }
            $saveNameArray = [];
            foreach ($params['file'] as $file) {
                $saveName = Filesystem::putFile('', $file);
                $saveNameArray[] = config('extra.app_url') . str_replace('\\', '/', 'uploads/' . $saveName);
            }
            result(['url' => $saveNameArray], 1, '上传成功');
        } else {
            if (!$validate->check($params)) {
                result(null, 0, $validate->getError());
            }
            $saveName = Filesystem::putFile('', $params['file']);
            $url = config('extra.app_url') . str_replace('\\', '/', 'uploads/' . $saveName);
            result(['url' => $url], 1, '上传成功');
        }
    }

    /**
     * @title 版本更新
     * @url /api/version_update
     * @method post
     *
     * @param name:system_type type:string require:1 desc:传android或者ios
     * @param name:version type:string require:1 desc:版本号
     *
     * @return version_info:版本信息@!
     * @return is_set_address:1设置了；2没有
     * @return cart_num:购物车数量
     * @version_info is_update:是否更新1更新；2不更新 download_url:下载地址 update_desc:更新描述 up_ios:苹果上架版本
     */
    public function versionUpdate()
    {
        $params = [
            'system_type' => input('system_type', 'android'),
            'version' => input('version', '0.0.0')
        ];
        $systemInfo = System::order('id', 'desc')->find();
        if ($params['system_type'] == 'android') {
            $res = version_compare($params['version'], $systemInfo['android_version']);
            $url = $systemInfo['android_url'];
        } elseif ($params['system_type'] == 'ios') {
            $res = version_compare($params['version'], $systemInfo['ios_version']);
            $url = $systemInfo['ios_url'];
        } else {
            result(null, 0, '未知的系统');
        }
        $isUpdate = $res == -1 ? 1 : 2;
        $updateDesc = $systemInfo['update_desc'];
        $upIos = $systemInfo['up_ios'];
        
        $versionInfo = [
            'is_update' => $isUpdate,
            'download_url' => $url,
            'update_desc' => $updateDesc,
            'up_ios' => $upIos
        ];
        result($versionInfo, 1, '获取信息成功');
    }

    /**
     * @title 关于我们
     * @url /api/about_us
     * @method post
     *
     * @return info:关于我们@!
     * @info title:app名称 about_us:内容 logo:logo
     */
    public function aboutUs()
    {
        $systemInfo = System::order('id', 'desc')->find();
        $info = [
            'title' => $systemInfo['web_name'],
            'logo' => $systemInfo['logo'],
            'about_us' => $systemInfo['about_us']
        ];
        result($info, 1, '获取信息成功');
    }
}
